﻿using ApiIOCService.Interface;
using Dal;
using Model;
using MongoService.Implement;
using MongoService.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ApiIOCService.Implement
{
    class CouponService : ICouponService
    {
        public CouponBatch GetCouponBatch(int batchId)
        {
            var result = new CouponBatch();
            var repository = new MongoRepository<CouponBatchEnity, string>();
            var model = repository.FirstOrDefault(x => x.BatchId == batchId);
            if (model != null)
            {
                result.AddTime = model.AddTime;
                result.ApplyDepartment = model.ApplyDepartment;
                result.BatchCode = model.BatchCode;
                result.BatchCreateType = model.BatchCreateType;
                result.BatchId = model.BatchId;
                result.BatchPrefix = model.BatchPrefix;
                result.CouponName = model.CouponName;
                result.CouponNumPerUser = model.CouponNumPerUser;
                result.CouponSettingId = model.CouponSettingId;
                result.CouponTotalNum = model.CouponTotalNum;
                result.ExpiredDate = model.ExpiredDate;
                result.ImpresetInvoiceId = model.ImpresetInvoiceId;
                result.InvalidMemo = model.InvalidMemo;
                result.InvalidStatus = model.InvalidStatus;
                result.InvalidTime = model.InvalidTime;
                result.InvalidUser = model.InvalidUser;
                result.OperatorId = model.OperatorId;
                result.SendType = model.SendType;
                result.Usage = model.Usage;
                return result;
            }

            result = CouponDAL.GetCouponBatch(batchId);
            if (result != null)
            {
                model = new CouponBatchEnity(result);
                var mongoInfo = repository.FirstOrDefault(x => x.BatchId == batchId);
                if (mongoInfo != null)
                {
                    model.Id = mongoInfo.Id;
                    repository.Update(model);
                }
                else
                    repository.Add(model);
            }
            return result;
        }

        public List<CouponBatch> GetCouponBatchs(List<int> batchIds)
        {
            return CouponDAL.GetCouponBatchs(batchIds);
        }

        public List<CouponBatch> GetCouponBatchs()
        {
            return CouponDAL.GetCouponBatchs();
        }
    }
}
